package com.crack.nga2.volley;

import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * 上传附加列表
 * Created by Administrator on 2016/1/7.
 */
public class UploadAttachDataList {
    public List<UploadAttach> attaches;
    //分割线
    public static final String BOUNDARY = "----WebKitFormBoundaryS6DeaMYJxz9uZWIc";


    boolean isLast = false;

    public byte[] toArray() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        for (int i = 0; i < attaches.size(); i++) {
            UploadAttach attach = attaches.get(i);
            isLast = i == attaches.size() - 1;
            StringBuilder sb = new StringBuilder();
            Map<String, String> map = attach.params;
            for (Map.Entry<String, String> entry : map.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                sb.append("--").append(BOUNDARY).append("\r\n");
                sb.append("Content-Disposition:form-data;").append("name=\"").append(key).append("\"").append("\r\n");
                sb.append("\r\n");
                sb.append(value).append("\r\n");
            }
            try {
                baos.write(sb.toString().getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
            File file = attach.file;
            try {
                addFile(baos, file, attach.no, attach.type);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("upload:55", e);
            }
        }
        return baos.toByteArray();

    }

    private void addFile(ByteArrayOutputStream baos, File file, int no, String type) throws IOException {
        String string = "--" + BOUNDARY + "\r\n";
        baos.write(string.getBytes());
        string = "Content-Disposition:form-data; name=\"attachment_file" + no + "\";filename=\"" + file.getName() + "\"\r\n";
        baos.write(string.getBytes("GBK"));
        string = "Content-Type:" + type + "\r\n";
        baos.write(string.getBytes());
        baos.write("\r\n".getBytes());
        InputStream is = null;
        byte[] bytes = new byte[1024 * 4];
        int length;
        try {
            is = new FileInputStream(file);
            while ((length = is.read(bytes)) != -1) {
                //写入文件数据
                baos.write(bytes, 0, length);
                Log.i("length", String.valueOf(length));
            }
            baos.write("\r\n".getBytes());
            if (isLast) {
                String end = "--" + BOUNDARY + "--";
                baos.write(end.getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
